
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Xml;
using OF.Security.Properties;

namespace OF.Security.Utils
{
	public static class XmlHelper
	{
		public delegate void DoNodeList(XmlNode nodeRoot, object oParam);

		private delegate void DeserializeCollectionAction(int index, object itemDate);

		public static XmlDocument GetExceptionXmlDoc(Exception ex)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(ex != null, "ex", new object[0]);
			XmlDocument xmlDocument = XmlHelper.CreateDomDocument("<ResponseError/>");
			XmlElement documentElement = xmlDocument.DocumentElement;
			XmlHelper.AppendExceptionNode(documentElement, ex);
			return xmlDocument;
		}

		private static void AppendExceptionNode(XmlNode root, Exception ex)
		{
			XmlHelper.AppendNode<string>(root, "Value", ex.Message);
			XmlHelper.AppendNode<string>(root, "Stack", ex.StackTrace);
			if (ex.InnerException != null)
			{
				XmlNode root2 = XmlHelper.AppendNode(root, "InnerException");
				XmlHelper.AppendExceptionNode(root2, ex.InnerException);
			}
		}

		public static void AppendExceptionInfo(XmlWriter writer, Exception ex)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(ex != null, "ex", new object[0]);
			writer.WriteStartElement("ResponseError");
			XmlHelper.AppendExceptionNode(writer, ex);
			writer.WriteEndElement();
		}

		private static void AppendExceptionNode(XmlWriter writer, Exception ex)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(writer != null, "writer", new object[0]);
			ExceptionHelper.FalseThrow<ArgumentNullException>(ex != null, "ex", new object[0]);
			XmlHelper.AppendNode<string>(writer, "Value", ex.Message);
			XmlHelper.AppendNode<string>(writer, "Stack", ex.StackTrace);
			if (ex.InnerException != null)
			{
				writer.WriteStartElement("InnerException");
				XmlHelper.AppendExceptionNode(writer, ex.InnerException);
				writer.WriteEndElement();
			}
		}

		public static XmlDocument LoadDocument(string path)
		{
			ExceptionHelper.CheckStringIsNullOrEmpty(path, "path");
			FileStream fileStream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
			XmlDocument result;
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileStream);
				result = xmlDocument;
			}
			finally
			{
				fileStream.Close();
			}
			return result;
		}

		public static XmlDocument CreateDomDocument(string xml)
		{
			ExceptionHelper.CheckStringIsNullOrEmpty(xml, "xml");
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(xml);
			return xmlDocument;
		}

		public static XmlNode AppendNode(XmlNode root, string nodeName)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(root != null, "root", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(nodeName, "nodeName");
			XmlNode xmlNode = root.OwnerDocument.CreateNode(XmlNodeType.Element, nodeName, string.Empty);
			root.AppendChild(xmlNode);
			return xmlNode;
		}

		public static XmlNode AppendNode<T>(XmlNode root, string nodeName, T nodeValue)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(root != null, "root", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(nodeName, "nodeName");
			XmlNode xmlNode = XmlHelper.AppendNode(root, nodeName);
			if (nodeValue != null)
			{
				xmlNode.InnerText = nodeValue.ToString();
			}
			return xmlNode;
		}

		public static XmlNode AppendNotNullNode<T>(XmlNode root, string nodeName, T nodeValue)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(root != null, "root", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(nodeName, "nodeName");
			string value = string.Empty;
			if (nodeValue != null)
			{
				value = nodeValue.ToString();
			}
			XmlNode xmlNode = null;
			if (!string.IsNullOrEmpty(value))
			{
				xmlNode = XmlHelper.AppendNode(root, nodeName);
				if (nodeValue != null)
				{
					xmlNode.InnerText = nodeValue.ToString();
				}
			}
			return xmlNode;
		}

		public static void AppendNode<T>(XmlWriter writer, string nodeName, T nodeValue)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(writer != null, "writer", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(nodeName, "nodeName");
			string value = string.Empty;
			if (nodeValue != null)
			{
				value = nodeValue.ToString();
			}
			writer.WriteElementString(nodeName, value);
		}

		public static void AppendNotNullNode<T>(XmlWriter writer, string nodeName, T nodeValue)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(writer != null, "writer", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(nodeName, "nodeName");
			string value = string.Empty;
			if (nodeValue != null)
			{
				value = nodeValue.ToString();
			}
			if (!string.IsNullOrEmpty(value))
			{
				writer.WriteElementString(nodeName, value);
			}
		}

		public static void AppendCDataNode<T>(XmlWriter writer, string nodeName, T nodeValue)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(writer != null, "writer", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(nodeName, "nodeName");
			string text = string.Empty;
			if (nodeValue != null)
			{
				text = nodeValue.ToString();
			}
			writer.WriteStartElement(nodeName);
			writer.WriteCData(text);
			writer.WriteEndElement();
		}

		public static XmlNode AppendCDataNode<T>(XmlNode root, string nodeName, T nodeValue)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(root != null, "root", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(nodeName, "nodeName");
			XmlNode xmlNode = root.OwnerDocument.CreateNode(XmlNodeType.Element, nodeName, string.Empty);
			root.AppendChild(xmlNode);
			if (nodeValue != null)
			{
				XmlCDataSection newChild = root.OwnerDocument.CreateCDataSection(nodeValue.ToString());
				xmlNode.AppendChild(newChild);
			}
			return xmlNode;
		}

		public static XmlNode AppendNotExistsNode<T>(XmlNode root, string nodeName, T nodeValue)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(root != null, "root", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(nodeName, "nodeName");
			XmlNode xmlNode = root.SelectSingleNode(nodeName);
			if (xmlNode == null)
			{
				xmlNode = XmlHelper.AppendNode(root, nodeName);
			}
			if (nodeValue != null && string.IsNullOrEmpty(xmlNode.InnerText))
			{
				xmlNode.InnerText = nodeValue.ToString();
			}
			return xmlNode;
		}

		public static XmlNode ReplaceNode<T>(XmlNode root, string nodeName, T nodeValue)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(root != null, "root", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(nodeName, "nodeName");
			XmlNode xmlNode = root.SelectSingleNode(nodeName);
			if (xmlNode == null)
			{
				xmlNode = XmlHelper.AppendNode<T>(root, nodeName, nodeValue);
			}
			else
			{
				xmlNode.InnerText = nodeValue.ToString();
			}
			return xmlNode;
		}

		public static XmlNode ReplaceNode(XmlNode root, string nodeName)
		{
			return XmlHelper.ReplaceNode<string>(root, nodeName, string.Empty);
		}

		public static XmlAttribute AppendAttr(XmlNode node, string attrName)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(node != null, "node", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(attrName, "attrName");
			XmlAttribute xmlAttribute = node.OwnerDocument.CreateAttribute(attrName);
			node.Attributes.SetNamedItem(xmlAttribute);
			return xmlAttribute;
		}

		public static XmlAttribute AppendAttr<T>(XmlNode node, string attrName, T attrValue)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(node != null, "node", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(attrName, "attrName");
			XmlAttribute xmlAttribute = XmlHelper.AppendAttr(node, attrName);
			if (attrValue != null)
			{
				xmlAttribute.Value = attrValue.ToString();
			}
			return xmlAttribute;
		}

		public static XmlAttribute AppendNotNullAttr<T>(XmlNode node, string attrName, T attrValue)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(node != null, "node", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(attrName, "attrName");
			string value = string.Empty;
			if (attrValue != null)
			{
				value = attrValue.ToString();
			}
			XmlAttribute xmlAttribute = null;
			if (!string.IsNullOrEmpty(value))
			{
				xmlAttribute = XmlHelper.AppendAttr(node, attrName);
				if (attrValue != null)
				{
					xmlAttribute.Value = value;
				}
			}
			return xmlAttribute;
		}

		public static void AppendAttr<T>(XmlWriter writer, string attrName, T attrValue)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(writer != null, "writer", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(attrName, "attrName");
			string value = string.Empty;
			if (attrValue != null)
			{
				value = attrValue.ToString();
			}
			writer.WriteAttributeString(attrName, value);
		}

		public static void AppendNotNullAttr<T>(XmlWriter writer, string attrName, T attrValue)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(writer != null, "writer", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(attrName, "attrName");
			string value = string.Empty;
			if (attrValue != null)
			{
				value = attrValue.ToString();
			}
			if (!string.IsNullOrEmpty(value))
			{
				writer.WriteAttributeString(attrName, value);
			}
		}

		public static void EnumNodeList(XmlNodeList nodeList, XmlHelper.DoNodeList nodeOP, object oParam)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(nodeList != null, "nodeList", new object[0]);
			if (nodeOP != null)
			{
				foreach (XmlNode nodeRoot in nodeList)
				{
					nodeOP(nodeRoot, oParam);
				}
			}
		}

		public static string GetSingleNodeText(XmlNode nodeRoot, string path)
		{
			return XmlHelper.GetSingleNodeValue<string>(nodeRoot, path, string.Empty);
		}

		public static T GetSingleNodeValue<T>(XmlNode nodeRoot, string path, T defaultValue)
		{
			T result = defaultValue;
			if (nodeRoot != null)
			{
				ExceptionHelper.CheckStringIsNullOrEmpty(path, "path");
				XmlNode xmlNode = nodeRoot.SelectSingleNode(path);
				if (xmlNode != null)
				{
					if (defaultValue == null)
					{
						result = DataConverter.ChangeType<string, T>(xmlNode.InnerText);
					}
					else
					{
						result = (T)((object)DataConverter.ChangeType<string>(xmlNode.InnerText, defaultValue.GetType()));
					}
				}
			}
			return result;
		}

		public static XmlNode GetSingleNodeException(XmlNode nodeRoot, string path)
		{
			return XmlHelper.GetSingleNodeException(nodeRoot, path, string.Empty);
		}

		public static XmlNode GetSingleNodeException(XmlNode nodeRoot, string path, string message)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(nodeRoot != null, "nodeRoot", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(path, "path");
			XmlNode xmlNode = nodeRoot.SelectSingleNode(path);
			if (string.IsNullOrEmpty(message))
			{
				message = string.Format(Resources.CanNotFindXmlNode, nodeRoot.Name, path);
			}
			ExceptionHelper.FalseThrow<ArgumentException>(xmlNode != null, message, new object[0]);
			return xmlNode;
		}

		public static string GetNodeText(XmlReader reader, string nodeName)
		{
			return XmlHelper.GetNodeValue<string>(reader, nodeName, string.Empty);
		}

		public static T GetNodeValue<T>(XmlReader reader, string nodeName, T defaultValue)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(reader != null, "reader", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(nodeName, nodeName);
			T result = defaultValue;
			if (!reader.IsEmptyElement)
			{
				try
				{
					reader.ReadStartElement(nodeName);
					if (defaultValue == null)
					{
						result = DataConverter.ChangeType<string, T>(reader.ReadString());
					}
					else
					{
						result = (T)((object)DataConverter.ChangeType<string>(reader.ReadString(), defaultValue.GetType()));
					}
					reader.ReadEndElement();
				}
				catch (XmlException)
				{
				}
			}
			else
			{
				try
				{
					reader.ReadStartElement(nodeName);
				}
				catch (XmlException)
				{
				}
			}
			return result;
		}

		public static string GetAttributeText(XmlReader reader, string attrName)
		{
			return XmlHelper.GetAttributeValue<string>(reader, attrName, string.Empty);
		}

		public static T GetAttributeValue<T>(XmlReader reader, string attrName, T defaultValue)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(reader != null, "reader", new object[0]);
			ExceptionHelper.CheckStringIsNullOrEmpty(attrName, "attrName");
			T result = defaultValue;
			string attribute = reader.GetAttribute(attrName);
			if (attribute != null)
			{
				if (defaultValue == null)
				{
					result = DataConverter.ChangeType<string, T>(attribute);
				}
				else
				{
					result = (T)((object)DataConverter.ChangeType<string>(attribute, defaultValue.GetType()));
				}
			}
			return result;
		}

		public static string GetAttributeText(XmlNode nodeRoot, string attrName)
		{
			return XmlHelper.GetAttributeValue<string>(nodeRoot, attrName, string.Empty);
		}

		public static T GetAttributeValue<T>(XmlNode nodeRoot, string attrName, T defaultValue)
		{
			T result = defaultValue;
			if (nodeRoot != null)
			{
				ExceptionHelper.CheckStringIsNullOrEmpty(attrName, "attrName");
				XmlNode namedItem = nodeRoot.Attributes.GetNamedItem(attrName);
				if (namedItem != null)
				{
					if (defaultValue == null)
					{
						result = DataConverter.ChangeType<string, T>(namedItem.Value);
					}
					else
					{
						result = (T)((object)DataConverter.ChangeType<string>(namedItem.Value, defaultValue.GetType()));
					}
				}
			}
			return result;
		}

	

		

	

	
	}
}
